package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.PayrollDto;
import xyz.zhouzhaodong.crm.dto.ReimburseDto;
import xyz.zhouzhaodong.crm.entity.Payroll;
import xyz.zhouzhaodong.crm.entity.Reimburse;
import xyz.zhouzhaodong.crm.mapper.ReimburseMapper;
import xyz.zhouzhaodong.crm.service.IReimburseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.FileUploadUtil;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 报销 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-21
 */
@Service
public class ReimburseServiceImpl extends ServiceImpl<ReimburseMapper, Reimburse> implements IReimburseService {

    @Resource
    ReimburseMapper reimburseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addReimburse(Reimburse reimburse) {
        try {
            // 新增
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            reimburse.setId(id);
            reimburse.setCreateUser(reimburse.getStaffId());
            reimburse.setCreateTime(new Date());
            reimburseMapper.insert(reimburse);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteReimburse(Reimburse reimburse) {
        try {
            Reimburse r = new Reimburse();
            r.setId(reimburse.getId());
            r.setFlag(1);
            r.setUpdateUser(reimburse.getStaffId());
            r.setUpdateTime(new Date());
            reimburseMapper.updateById(r);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateReimburse(Reimburse reimburse) {
        try {
            reimburse.setUpdateUser(reimburse.getStaffId());
            reimburse.setUpdateTime(new Date());
            reimburse.setStaffId(null);
            reimburseMapper.updateById(reimburse);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectReimburse(ReimburseDto reimburseDto) {
        if(reimburseDto.getPageNo() == 0 && reimburseDto.getPageSize() == 0){
            List<ReimburseDto> list = reimburseMapper.selectReimburse(reimburseDto);
            for(ReimburseDto r : list){
                String imgAddress = r.getImgAddress();
                String base64 = FileUploadUtil.changeBase64(imgAddress);
                r.setBase64(base64);
            }
            return RestMessage.success(list);
        } else {
            Page<ReimburseDto> page = new Page<>(reimburseDto.getPageNo(), reimburseDto.getPageSize());
            String type = reimburseDto.getType();
            if(type != null && !"".equals(type)){
                StringBuilder stringBuilder = new StringBuilder("(");
                String[] str = type.split(",");
                for(String s : str){
                    stringBuilder.append("'").append(s).append("',");
                }
                String all = stringBuilder.substring(0, stringBuilder.length() - 1) + ")";
                reimburseDto.setType(all);
            }
            IPage<ReimburseDto> list = reimburseMapper.selectReimburse(reimburseDto, page);
            for(ReimburseDto r : list.getRecords()){
                String imgAddress = r.getImgAddress();
                if(imgAddress != null && !"".equals(imgAddress)){
                    String base64 = FileUploadUtil.changeBase64(imgAddress);
                    r.setBase64(base64);
                }
            }
            return RestMessage.success(list);
        }
    }
}
